package com.company.JUC.ThreadPool.ThreadPoolExecutorTest;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName MyMonitorThread
 * @Description TODO  监视executor当前状态
 * @Author ylr
 * @Date 2021/7/22 10:14
 * @Version 1.0
 */
public class MyMonitorThread implements Runnable{
    private ThreadPoolExecutor executor;
    private int seconds;
    private boolean isRun = true;

    public MyMonitorThread(ThreadPoolExecutor threadPoolExecutor, int delay) {
        this.executor = threadPoolExecutor;
        this.seconds = delay;
    }
    public void shutdown(){
        this.isRun =false;
    }



    @Override
    public void run() {
        while (isRun){
            System.out.println(
                    String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                            this.executor.getPoolSize(),
                            this.executor.getCorePoolSize(),
                            this.executor.getActiveCount(),
                            this.executor.getCompletedTaskCount(),
                            this.executor.getTaskCount(),
                            this.executor.isShutdown(),
                            this.executor.isTerminated()));
            try {
                Thread.sleep(seconds*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }




    }
}
